<template>
  <div class="wrapper" ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  export default {
    props: {
      probeType: {
        type: Number,
        default: 3
      },
      click: {
        type: Boolean,
        default: true
      },
      tap: {
        type: Boolean,
        default: false
      },
      data: {
        type: Array,
        default: null
      },
      scrollX: {
        type: Boolean,
        default: false
      },
      scrollY: {
        type: Boolean,
        default: true
      },
      preventDefault: {
        type: Boolean,
        default: true
      },
      refreshDelay: {
        type: Number,
        default: 20
      }
    },
    mounted() {
      this.$vgo.on('refresh:scroll', () => {
        setTimeout(() => {
        this.refresh()
        }, 1000)
      })

      this.$nextTick(() => {
        setTimeout(() => {
          this.initScroll()
        }, 1000)
      })
    },
    methods: {
      initScroll() {
        if (!this.$refs.wrapper) {
          return
        }

        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType: this.probeType,
          preventDefault: this.preventDefault,
          click: this.click,
          tap: this.tap,
          scrollX: this.scrollX,
          scrollY: this.scrollY
        })
      },
      enable() {
        this.scroll && this.scroll.enable()
      },
      disable() {
        this.scroll && this.scroll.disable()
      },
      refresh() {
        this.scroll && this.scroll.refresh()
      },
      scrollTo() {
        this.scroll && this.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement() {
        this.scroll && this.scrollToElement(this.scroll, arguments)
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this.initScroll()
        }, this.refreshDelay)
      }
    }
  }
</script>

